<?php
require_once('pworks/mvc/action/BaseAction.class.php');
require_once "xpface/util/XPFaceConfigLoader.class.php";
require_once "xpface/util/XPFace.class.php";
require_once "xpface/util/data/DataSourceFactory.class.php";

class RegionAction extends BaseAction{
    
    /**
	 * @isnull false
	 */
    public $page_id;
    public $region;
    
    public function execute(){
        //xpface config
        $xpFaceConfigLoader = XPFaceConfigLoader::getInstance();
        $xpFaceConfig = $xpFaceConfigLoader->getXPFaceConfig();
        //var_dump($xpFaceConfig);
        
        //page config
        $pageConfig = $xpFaceConfigLoader->getPageConfig();
        //var_dump($pageConfig);
        
        //init tpl file
        $pythonCommand = $xpFaceConfig["python_command"]->value;
        $parseScript = $xpFaceConfig["parse_script"]->value;
        $sourceTplPath = $xpFaceConfig["source_tpl_path"]->value;
        $parsedTplPath = $xpFaceConfig["parsed_tpl_path"]->value;
        
        //parse tpl file
        $currentPageConfig = $pageConfig[$this->page_id];
        
        //parse region module
        $sourceUIModule = $currentPageConfig->regions[$this->region];
        $regionId = $this->region;
        $sourceTplFile = $sourceTplPath . "/" . $sourceUIModule;
        $copiedSourceFile = $parsedTplPath . "/" . $sourceUIModule;
        $parsedTplFile = $parsedTplPath . "/" . $sourceUIModule . ".php";
        
        if (file_exists($sourceTplFile)) {
            if (!file_exists($copiedSourceFile) || md5_file($copiedSourceFile) != md5_file($sourceTplFile)) {
                copy($sourceTplFile, $copiedSourceFile);
                if (file_exists($parsedTplFile)) {
                    unlink($parsedTplFile);
                }
                //var_dump("python D:/python/system/ParseTags.py {$sourceTplFile} {$parsedTplFile}");
                $cmd = "{$pythonCommand} {$parseScript} {$sourceTplFile} {$parsedTplFile}";
                //var_dump($cmd);
                exec($cmd);
                if (!file_exists($parsedTplFile)) {
                    echo "File '{$parsedTplFile}' parse failing! ";
                    exit;
                }
            }
        } else {
            echo "File '{$sourceTplFile}' not exists! ";
            exit;
        }
        
        //datasource
        $binds = array();
        if (count($currentPageConfig->binds) > 0) {
            foreach ($currentPageConfig->binds as $bindKey => $bindValue) {
                //var_dump($bindKey, $bindValue);
                $binds[$bindKey] = $bindValue;
            }
        }
        $this->setData("binds", $binds);
        
        //module
        $uimoduleSrc = $currentPageConfig->regions[$regionId];
        $dataSource = $xpFaceConfigLoader->getDataSourceConfig($uimoduleSrc);
        //var_dump($dataSource);
        if ($dataSource !== null) {
            //var_dump($dataSource->data);
            $data = array();
            foreach ($dataSource->data as $key => $source) {
                $data[$key] = $source;
            }
            $this->setData(md5($dataSource->uimodule), $data);
        }
        
        //out put
        XPFace::setAction($this);
        XPFace::getRegion($regionId);
    }
}
